package co.th.bjc.md.online.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import co.th.bjc.md.dbonline.bean.LoginDBOnlineBean;
import co.th.bjc.md.dbonline.control.LoginDBOnlineControl;
import co.th.bjc.md.online.constant.OnlineConstant;

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger _logger = Logger.getLogger(LoginServlet.class);
	String forwardPage = "";
       
    public LoginServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		String transaction_id = (String) session.getAttribute("Transaction_id");
		try{
			String action = request.getParameter("action");
			_logger.debug(transaction_id + "Action : " + action);
			
			if(action == null){
				getServletContext().getRequestDispatcher(OnlineConstant.JSP.LOGIN).forward(request, response);
				
			}else if (action.equalsIgnoreCase("Login")){
				this.login(transaction_id, request, response);
				
			}else if (action.equalsIgnoreCase("Logout")){
				this.logout(request, response);
				
			}else if (action.equalsIgnoreCase("Forget")){
				this.forget(request, response);
				
			}else if (action.equalsIgnoreCase("Register")){
				this.register(request, response);
				
			}else{
				getServletContext().getRequestDispatcher(OnlineConstant.JSP.LOGIN).forward(request, response);
				
			}
			
		}catch(Exception ex){
			_logger.error(transaction_id + "Exception : " + ex.getMessage());
		}
	}
	
	
	
	private void login(String transaction_id, HttpServletRequest request, HttpServletResponse response){
		try{
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			
			LoginDBOnlineBean bean = new LoginDBOnlineBean(username, password);
			LoginDBOnlineControl control = new LoginDBOnlineControl();
			bean = control.verifyLoginDBOnline(bean);
			
			_logger.debug(transaction_id + "- Username : " + username + " <> Password : " + password);
			
			if (bean == null){
				throw new Exception("Request invalid");
				
			}else if (OnlineConstant.FLAG_Y.equalsIgnoreCase(bean.getActive())){
				HttpSession session = request.getSession();
			    session.setAttribute("userID", username);
			    forwardPage = OnlineConstant.REDIRECT.DATA_BUSINESS_SERVLET;
			    
			}else{
			    forwardPage = OnlineConstant.REDIRECT.DATA_BUSINESS_SERVLET;
			}

			_logger.debug("Status User : " + bean.getActive());
		}catch(Exception ex){
			_logger.error(LoginServlet.class.getName()+" : "+ ex);
		}finally{
			try {
				_logger.info(request.getContextPath() + forwardPage);
				response.sendRedirect(request.getContextPath() + forwardPage);
//	            request.getRequestDispatcher(forwardPage).forward(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void logout(HttpServletRequest request, HttpServletResponse response){
		try{
			HttpSession session = request.getSession();
			session.invalidate();
			response.sendRedirect("LoginServlet");
		}catch(Exception ex){
			_logger.error(LoginServlet.class.getName()+" : "+ ex);
		}finally{
			try {
				response.sendRedirect(forwardPage);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private void forget(HttpServletRequest request, HttpServletResponse response){
		try{
			
		}catch(Exception ex){
			_logger.error(LoginServlet.class.getName()+" : "+ ex);
		}finally{
			try {
				response.sendRedirect(forwardPage);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private void register(HttpServletRequest request, HttpServletResponse response){
		try{
			
		}catch(Exception ex){
			_logger.error(LoginServlet.class.getName()+" : "+ ex);
		}finally{
			try {
				response.sendRedirect(forwardPage);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
